package com.snailvr.tv.provider;

import android.util.Log;

import com.snail.readyvr.util.EventBus;
import com.snail.readyvr.util.Utils;
import com.snailvr.tv.Definition;
import com.snailvr.tv.event.LoadFailureEvent;
import com.snailvr.tv.network.RetrofitFactory;
import com.snailvr.tv.network.entity.JXListResponBean;
import com.snailvr.tv.network.entity.PlayUrlResponBean;
import com.snailvr.tv.network.entity.RoomInfoResponBean;
import com.snailvr.tv.network.entity.RoomListResponBean;
import com.snailvr.tv.network.service.JXListService;
import com.snailvr.tv.network.service.RoomInfoService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/2/10.
 */

public class RoomInfoProvider {
    private RoomInfoService mRoomInfoService;
    private JXListService mJXListService;

    public RoomInfoProvider(){
        mRoomInfoService = RetrofitFactory.getmRoomInfoService();
        mJXListService = RetrofitFactory.getmJXListService();
    }

    public void getRoomInfo(int room_id) {
        Call<RoomInfoResponBean> call = mRoomInfoService.getRoomInfo(Utils.sessionid,Utils.token,room_id);
        call.enqueue(new Callback<RoomInfoResponBean>() {
            @Override
            public void onResponse(Call<RoomInfoResponBean> call, Response<RoomInfoResponBean> response) {
                if(response.body()!=null) {
                    RoomInfoResponBean _Bean = response.body();
                    Log.i("App","message" + _Bean.getMessage());
                    if(_Bean.getCode() == 0) {
//                        ChannelPlayingEvent _Event = new ChannelPlayingEvent(_Bean);
                        EventBus.getDefault().post(Definition.BusTag.ROOM_INFO_TAG,_Bean.getData());
                    } else {
                        EventBus.getDefault().post(Definition.BusTag.ROOM_INFO_TAG,new LoadFailureEvent(_Bean.getCode(),_Bean.getMessage()));
                    }
                }
            }

            @Override
            public void onFailure(Call<RoomInfoResponBean> call, Throwable t) {
                EventBus.getDefault().post(Definition.BusTag.ROOM_INFO_TAG,new LoadFailureEvent(0,"获取当前房间信息失败"));
            }
        });
    }

    public void getRoomList(int channel_id, int page, int pageSize) {
        Call<RoomListResponBean> call = mRoomInfoService.getRoomList(Utils.sessionid,Utils.token,channel_id,page,pageSize);
        call.enqueue(new Callback<RoomListResponBean>() {
            @Override
            public void onResponse(Call<RoomListResponBean> call, Response<RoomListResponBean> response) {
                if(response.body()!=null) {
                    RoomListResponBean _Bean = response.body();
                    Log.i("App","message" + _Bean.getMessage());
                    if(_Bean.getCode() == 0) {
//                        ChannelPlayingEvent _Event = new ChannelPlayingEvent(_Bean);
                        EventBus.getDefault().post(Definition.BusTag.ROOM_LIST_INFO_TAG,_Bean);
                    } else {
                        EventBus.getDefault().post(Definition.BusTag.ROOM_LIST_INFO_TAG,new LoadFailureEvent(_Bean.getCode(),_Bean.getMessage()));
                    }
                }
            }

            @Override
            public void onFailure(Call<RoomListResponBean> call, Throwable t) {
                EventBus.getDefault().post(Definition.BusTag.ROOM_LIST_INFO_TAG,new LoadFailureEvent(0,"获取房间列表信息失败"));
            }
        });
    }



    public void getPlayUrl(int room_id) {
        Call<PlayUrlResponBean> call = mRoomInfoService.getPlayUrl(Utils.sessionid,Utils.token,room_id);
        call.enqueue(new Callback<PlayUrlResponBean>() {
            @Override
            public void onResponse(Call<PlayUrlResponBean> call, Response<PlayUrlResponBean> response) {
                if(response.body()!=null) {
                    PlayUrlResponBean _Bean = response.body();
                    Log.i("App","message" + _Bean.getMessage());
                    if(_Bean.getCode() == 0) {
//                        ChannelPlayingEvent _Event = new ChannelPlayingEvent(_Bean);
                        EventBus.getDefault().post(Definition.BusTag.PLAYING_TAG,_Bean);
                    } else {
                        EventBus.getDefault().post(Definition.BusTag.PLAYING_TAG,new LoadFailureEvent(_Bean.getCode(),_Bean.getMessage()));
                    }
                }
            }

            @Override
            public void onFailure(Call<PlayUrlResponBean> call, Throwable t) {
                EventBus.getDefault().post(Definition.BusTag.PLAYING_TAG,new LoadFailureEvent(0,"获取当前房间播放URL信息失败"));
            }
        });
    }

    public void getJXList(int page, int pageSize) {
        Call<JXListResponBean> call = mJXListService.getJXList(page,pageSize);
        call.enqueue(new Callback<JXListResponBean>() {
            @Override
            public void onResponse(Call<JXListResponBean> call, Response<JXListResponBean> response) {
                if(response.body()!=null) {
                    JXListResponBean _Bean = response.body();
                    Log.i("App","message" + _Bean.getMsg());
                    if(_Bean.getCode() == 0) {
                        EventBus.getDefault().post(Definition.BusTag.JX_TAG,_Bean);
                    } else {
                        EventBus.getDefault().post(Definition.BusTag.JX_TAG,new LoadFailureEvent(_Bean.getCode(),_Bean.getMsg()));
                    }
                }
            }

            @Override
            public void onFailure(Call<JXListResponBean> call, Throwable t) {
                EventBus.getDefault().post(Definition.BusTag.PLAYING_TAG,new LoadFailureEvent(0,"获取精选下载失败"));
            }
        });
    }
}
